home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / util / properti.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.9 KB  |  263 lines

  1. /*
  2.  * @(#)Properties.java    1.19 95/08/30 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.util;
  21.  
  22. import java.io.IOException;
  23. import java.io.PrintStream;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;
  26. import java.util.Hashtable;
  27.  
  28. /**
  29.  * Persistent properties class. This class is basically a hashtable 
  30.  * that can be saved/loaded from a stream. If a property is not found,
  31.  * a property list containing defaults is searched. This allows
  32.  * arbitrary nesting.
  33.  *
  34.  * @author Arthur van Hoff
  35.  * @version     1.19, 08/30/95
  36.  */
  37. public
  38. class Properties extends Hashtable {
  39.     protected Properties defaults;
  40.  
  41.     /**
  42.      * Creates an empty property list.
  43.      */
  44.     public Properties() {
  45.     this(null);
  46.     }
  47.  
  48.     /**
  49.      * Creates an empty property list with specified defaults.
  50.      * @param defaults the defaults
  51.      */
  52.     public Properties(Properties defaults) {
  53.     this.defaults = defaults;
  54.     }
  55.  
  56.     /**
  57.      * Loads properties from an InputStream.
  58.      * @param in the input stream
  59.      */
  60.     public synchronized void load(InputStream in) throws IOException {
  61.     in = Runtime.getRuntime().getLocalizedInputStream(in);
  62.  
  63.     int ch = in.read();
  64.     while (true) {
  65.         switch (ch) {
  66.           case -1:
  67.         return;
  68.  
  69.           case '#':
  70.           case '!':
  71.         do {
  72.             ch = in.read();
  73.         } while ((ch >= 0) && (ch != '\n') && (ch != '\r'));
  74.         continue;
  75.  
  76.           case '\n':
  77.           case '\r':
  78.           case ' ':
  79.           case '\t':
  80.         ch = in.read();
  81.         continue;
  82.         }
  83.  
  84.         // Read the key
  85.         StringBuffer key = new StringBuffer();
  86.         while ((ch >= 0) && (ch != '=') && (ch != ':') && 
  87.            (ch != ' ') && (ch != '\t') && (ch != '\n') && (ch != '\r')) {
  88.         key.append((char)ch);
  89.         ch = in.read();
  90.         }
  91.         while ((ch == ' ') && (ch == '\t')) {
  92.         ch = in.read();
  93.         }
  94.         if ((ch == '=') || (ch == ':')) {
  95.         ch = in.read();
  96.         }
  97.         while ((ch == ' ') && (ch == '\t')) {
  98.         ch = in.read();
  99.         }
  100.  
  101.         // Read the value
  102.         StringBuffer val = new StringBuffer();
  103.         while ((ch >= 0) && (ch != '\n') && (ch != '\r')) {
  104.         if (ch == '\\') {
  105.             switch (ch = in.read()) {
  106.               case '\n': 
  107.             while (((ch = in.read()) == ' ') || (ch == '\t'));
  108.             continue;
  109.               case 't': ch = '\t'; break;
  110.               case 'n': ch = '\n'; break;
  111.               case 'r': ch = '\r'; break;
  112.               case 'u': {
  113.             while ((ch = in.read()) == 'u');
  114.             int d = 0;
  115.               loop:
  116.             for (int i = 0 ; i < 4 ; i++, ch = in.read()) {
  117.                 switch (ch) {
  118.                   case '0': case '1': case '2': case '3': case '4':
  119.                   case '5': case '6': case '7': case '8': case '9':
  120.                 d = (d << 4) + ch - '0';
  121.                 break;
  122.                   case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  123.                 d = (d << 4) + 10 + ch - 'a';
  124.                 break;
  125.                   case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  126.                 d = (d << 4) + 10 + ch - 'A';
  127.                 break;
  128.                   default:
  129.                 break loop;
  130.                 }    
  131.             }
  132.             ch = d;
  133.               }
  134.             }
  135.         }
  136.         val.append((char)ch);
  137.         ch = in.read();
  138.         }
  139.  
  140.         //System.out.println(key + " = '" + val + "'");
  141.         put(key.toString(), val.toString());
  142.     }
  143.     }
  144.  
  145.     /**
  146.      * Save properties to an OutputStream. Use the header as
  147.      * a comment at the top of the file.
  148.      */
  149.     public synchronized void save(OutputStream out, String header) {
  150.     OutputStream localOut = Runtime.getRuntime().getLocalizedOutputStream(out);
  151.     PrintStream prnt = new PrintStream(localOut, false);
  152.     boolean localize = localOut != out;
  153.  
  154.     if (header != null) {
  155.         prnt.write('#');
  156.         prnt.println(header);
  157.     }
  158.     prnt.write('#');
  159.     prnt.println(new Date());
  160.  
  161.     for (Enumeration e = keys() ; e.hasMoreElements() ;) {
  162.         String key = (String)e.nextElement();
  163.         prnt.print(key);
  164.         prnt.write('=');
  165.  
  166.         String val = (String)get(key);
  167.         int len = val.length();
  168.         boolean empty = false;
  169.  
  170.         for (int i = 0 ; i < len ; i++) {
  171.         int ch = val.charAt(i);
  172.  
  173.         switch (ch) {
  174.           case '\\': prnt.write('\\'); prnt.write('\\'); break;
  175.           case '\t': prnt.write('\\'); prnt.write('t'); break;
  176.           case '\n': prnt.write('\\'); prnt.write('n'); break;
  177.           case '\r': prnt.write('\\'); prnt.write('r'); break;
  178.  
  179.           default:
  180.             if ((ch < ' ') || (ch >= 127) || (empty && (ch == ' '))) {
  181.             if ((ch > 255) && localize) {
  182.                 prnt.write(ch);
  183.             } else {
  184.                 prnt.write('\\');
  185.                 prnt.write('u');
  186.                 prnt.write((ch >> 12) & 0xF);
  187.                 prnt.write((ch >>  8) & 0xF);
  188.                 prnt.write((ch >>  4) & 0xF);
  189.                 prnt.write((ch >>  0) & 0xF);
  190.             }
  191.             } else {
  192.             prnt.write(ch);
  193.             }
  194.         }
  195.         empty = false;
  196.         }
  197.         prnt.write('\n');
  198.     }
  199.     }
  200.  
  201.     /**
  202.      * Gets a property with the specified key. If the key is not 
  203.      * found in this property list, try the defaults. This method 
  204.      * returns null if the property is not found.
  205.      * @param key the hashtable key
  206.      */
  207.     public String getProperty(String key) {
  208.     String val = (String)super.get(key);
  209.     return ((val == null) && (defaults != null)) ? defaults.getProperty(key) : val;
  210.     }
  211.  
  212.     /**
  213.      * Gets a property with the specified key and default. If the 
  214.      * key is not found in this property list, try the defaults. 
  215.      * This method returns def if the property is not found.
  216.      */
  217.     public String getProperty(String key, String defaultValue) {
  218.     String val = getProperty(key);
  219.     return (val == null) ? defaultValue : val;
  220.     }
  221.  
  222.     /**
  223.      * Enumerates all the keys.
  224.      */
  225.     public Enumeration propertyNames() {
  226.     Hashtable h = new Hashtable();
  227.     enumerate(h);
  228.     return h.keys();
  229.     }
  230.  
  231.     /**
  232.      * List properties, for debugging
  233.      */
  234.     public void list(PrintStream out) {
  235.     out.println("-- listing properties --");
  236.     Hashtable h = new Hashtable();
  237.     enumerate(h);
  238.     for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
  239.         String key = (String)e.nextElement();
  240.         String val = (String)h.get(key);
  241.         if (val.length() > 40) {
  242.         val = val.substring(0, 37) + "...";
  243.         }
  244.         out.println(key + "=" + val);
  245.     }
  246.     
  247.     }
  248.     
  249.     /**
  250.      * Enumerates all key/value pairs in the specified hastable.
  251.      * @param h the hashtable
  252.      */
  253.     private synchronized void enumerate(Hashtable h) {
  254.     if (defaults != null) {
  255.         defaults.enumerate(h);
  256.     }
  257.     for (Enumeration e = keys() ; e.hasMoreElements() ;) {
  258.         String key = (String)e.nextElement();
  259.         h.put(key, get(key));
  260.     }
  261.     }
  262. }
  263.